/*	TargetedAbundanceAnalysisReader.java
 *
 *	Copyright (c) Christopher Jacobs 2010
 *
 *	This file is part of Manatee ('this project').
 *
 *	This project is free software: you can redistribute it and/or modify it
 *	under the terms of the GNU General Public License as published by the Free
 *	Software Foundation, either version 3 of the License, or (at your option)
 *	any later version.
 *
 *	This project is distributed in the hope that it will be useful, but WITHOUT
 *	ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *	FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 *	more details.
 *
 *	You should have received a copy of the GNU General Public License along with
 *	this project. If not, see <http://www.gnu.org/licenses/>.
 */
package edu.bu.manateem.msg.analysis.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 *	<p>
 *	
 *	</p>
 *
 *
 *	@author	Christopher Jacobs
 *
 *	@version	1.0
 */
public class TargetedAbundanceAnalysisReader
{
	/*
	 * TYPES
	 */

	private enum ScannerMode
	{
		NONE,
		RAW,
		TARGET,
		OPTION;
	}
	
	/*
	 * CONSTANTS
	 */

	/*
	 * FIELDS
	 */

	/*
	 * CONSTRUCTORS
	 */

	/*
	 * INITIALIZERS
	 */

	/*
	 * METHODS
	 */

	public static TargetedAbundanceAnalysisArgs readFile(String file)
	{
		try
		{
			List<String> args = new ArrayList<String>();
			
			Scanner scanner = new Scanner(new File(file));
			ScannerMode mode = ScannerMode.NONE;
			while (scanner.hasNextLine())
			{
				String line = scanner.nextLine();
				if (line.equals("") || line.matches("^\\s+$"))
				{
					// skip
				}
				else if (line.equalsIgnoreCase("Raw Input Files:"))
				{
					mode = ScannerMode.RAW;
				}
				else if (line.equalsIgnoreCase("Targets File:"))
				{
					mode = ScannerMode.TARGET;
				}
				else if (line.equalsIgnoreCase("Options:"))
				{
					mode = ScannerMode.OPTION;
				}
				else
				{
					switch (mode)
					{
						case RAW:
							args.add("--raw=" + line);
							break;
						case TARGET:
							args.add("--target=" + line);
							break;
						case OPTION:
							if (line.matches("R.T. Sum Width (Left):*"))
							{
								args.add("--rtSum=" + line.substring(23));
							}
							else if (line.matches("R.T. Search Width (Left):*"))
							{
								args.add("--rtSearch=" + line.substring(26));
							}
							else if (line.matches("m/z Sum Width (Left):*"))
							{
								args.add("--mzLeft=" + line.substring(22));
							}
							else if (line.matches("m/z Sum Width (Right):*"))
							{
								args.add("--mzRight=" + line.substring(23));
							}
							break;
					}
				}
			}
			
			TargetedAbundanceAnalysisArgs taaArgs =
				new TargetedAbundanceAnalysisArgs();
			taaArgs.parse(args);
			return taaArgs;
		}
		catch (FileNotFoundException x)
		{
			return null;
		}
		
	}
}
